/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.dusa.switco;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author vdusa
 */
public class ConvexCostFunctionFactory {
    private static ConvexCostFunctionFactory instance = null;

    private ConvexCostFunctionFactory(){}

    public static ConvexCostFunctionFactory getInstance(){
        if(instance == null){
            instance = new ConvexCostFunctionFactory();
        }
        return instance;
    }

    /**
     * Use this function to create automatically correct ConvexCostFunction object
     * @param costFunction
     * @return cost function object corresponding with given string. If the string is
     * [number;number;number], then {@link QuadraticCostFunction} is returned. Otherwise {@link DefaultConvexCostFunction}
     * is returned.
     */
    public ConvexCostFunction getConvexCostFunction(String costFunction){
        String pattern = "\\[(-?\\d*\\.*\\d*);(-?\\d*\\.*\\d*);(-?\\d*\\.*\\d*)\\]";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(costFunction);
        if(m.find()){
            return new QuadraticCostFunction(new Double(m.group(1)),new Double(m.group(2)),new Double(m.group(3)));
        } else{
            return new DefaultConvexCostFunction(costFunction);
        }
    }
}
